package com.chookss.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.chookss.HomeActivityss;
import com.chookss.Urls;
import com.chookss.WebCommonActivity;
import com.chookss.base.Token;
import com.chookss.home.NavigationBaseActivity;
import com.chookss.home.audit.clockIn.ClockInSureActivity;
import com.chookss.home.interview.InterviewRecordsActivity;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;

import java.util.HashMap;
import java.util.Map;

public class NotificationClickReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //todo 跳转之前要处理的逻辑
        String pushMessageId = "", type = "", code = "", addressType = "";
        if (intent == null) {
            return;
        } else {
            pushMessageId = intent.getStringExtra("pushMessageId");
            type = intent.getStringExtra("type");
            code = intent.getStringExtra("code");
        }
        if ("1".equals(type)) {
            if (Utils.isNull(code)) {
                intent = new Intent(context, NavigationBaseActivity.class);
                intent.putExtra("type", 1);
            } else {
                intent = new Intent(context, WebCommonActivity.class);
                if (Urls.baseUrl.contains("dev_kmer")) {
                    addressType = "1";
                } else {
                    addressType = "0";
                }
                String url = Urls.webBaseUrl + "indexNotice.html#/noticeDetail?noticeCode=" + code + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
                intent.putExtra("url", url);
                intent.putExtra("waterMark", "12");
            }
        } else if ("2".equals(type)) {
            if (Utils.isNull(code)) {
                intent = new Intent(context, HomeActivityss.class);
            } else {
                intent = new Intent(context, WebCommonActivity.class);
                if (Urls.baseUrl.contains("dev_kmer")) {
                    addressType = "1";
                } else {
                    addressType = "0";
                }
                String url = Urls.webBaseUrl + "indexNew.html#/newDetail?newsCode=" + code + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
                intent.putExtra("url", url);
                intent.putExtra("waterMark", "13");
            }
        } else if ("3".equals(type)) {
            intent = new Intent(context, NavigationBaseActivity.class);
            intent.putExtra("type", 5);
        } else if ("4".equals(type)) {
            if (Utils.isNull(code)) {
                intent = new Intent(context, HomeActivityss.class);
            } else {
                intent = new Intent(context, WebCommonActivity.class);
                intent.putExtra("title", "详情");
                intent.putExtra("url", code);
            }

        } else if ("5".equals(type)) {
            intent = new Intent(context, InterviewRecordsActivity.class);
            intent.putExtra("type", 2);
        } else if ("6".equals(type)) {
            if (Utils.isNull(code)) {
                intent = new Intent(context, HomeActivityss.class);
            } else {
                if(code.length()>11){
                    intent = new Intent(context, ClockInSureActivity.class);
                    String time=code.substring(6,12);
                    intent.putExtra("time",time.substring(0,4)+"-"+ time.substring(4,6));
                    intent.putExtra("noticeCode", code);
                }else{
                    intent = new Intent(context, HomeActivityss.class);
                }


            }
        }  else {
            intent = new Intent(context, HomeActivityss.class);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        updatePushRead(pushMessageId);
    }

    public void updatePushRead(String pushMessageId) {
        if (Utils.isNull(pushMessageId)) {
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("pushMessageId", pushMessageId);
        MyHttpRequest.getDefault().getRequestCompat(null, Urls.updatePushRead, SignUtils2.makeSignStr(map), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) {
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
    }
}
